<?php
declare(strict_types=1);

namespace app\api\controller;

use app\basic\ApiController;
use app\api\logic\EmailLogic;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("邮件服务")
 */
class EmailController extends ApiController
{
    protected $logic;

    public function __construct()
    {
        $this->logic = new EmailLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("发送文档邮件")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("doc_id", type="int", require=true, desc="文档ID",example="1")
     * @Apidoc\Param("email", type="string", require=true, desc="接收邮箱",example="1234567890@qq.com")
     */
    public function sendDocEmail()
    {
        // 验证用户是否登录
        if (!$this->auth->isLogin()) {
            return $this->fail('请先登录');
        }
        $params = $this->request->only(['doc_id', 'email']);
        // 发送邮件
        $result = $this->logic->sendDocEmail(
            $this->auth->getUserId(),
            $params['doc_id'],
            $params['email']
        );

        if (!$result) {


            return $this->fail($this->logic->getError());
        }

        return $this->success('邮件发送成功');
    }
}